
package com.afd.ozblip.activity;

import android.app.Activity;
import android.app.Application;

import com.afd.ozblip.db.DBHelper;
import com.afd.ozblip.photo.TempFileProvider;
import com.j256.ormlite.android.apptools.OpenHelperManager;

import java.util.LinkedList;
import java.util.List;

/**
 * OzBlip Activity管理类
 * 
 * @author 徐鹏
 */
public class OzBlipApplication extends Application {

    private List<Activity> mList = new LinkedList<Activity>();
    private static OzBlipApplication instance;
    static DBHelper mDBHelper;

    /**
     * 重载方法
     */
    @Override
    public void onCreate() {
        super.onCreate();
        initDb();
        TempFileProvider.init(getString(R.string.app_temp_file_provider_authorities));
    }

    private void initDb() {
        mDBHelper = OpenHelperManager.getHelper(this, DBHelper.class);
    }

    public DBHelper getDBHelper() {
        if (mDBHelper == null) {
            initDb();
        }
        return mDBHelper;
    }

    public synchronized static OzBlipApplication getInstance() {
        if (null == instance) {
            instance = new OzBlipApplication();
        }

        return instance;
    }

    /**
     * 添加Activity
     * 
     * @param activity
     */
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    /**
     * 退出整个程序
     */
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }
}
